<?php 
	include_once "header.php";
	
	$playerid = $current_game['turn'];
	if (isset($_REQUEST['player'])) {
		$playerid = $_REQUEST['player'];
	}
	$player = mysql_fetch_assoc(mysql_query("SELECT * FROM `player` WHERE `id` = '$playerid' LIMIT 1"));
?>

<h2><?= $player['name']; ?>'s Cash</h2>

<table class="report" id="report">
	<tr>
		<th>Date</th>
		<th>Cash</th>
		<th>Comment</th>
		<th>Career</th>
		<th>Player to</th>
		<th>Total</th>
	</tr>
<?php
	$transactions_query = mysql_query("
		SELECT `cash`.*, `player`.`name` AS `toplayername`, `career`.`name` AS `tocareername`
		FROM `cash` 
			LEFT JOIN `player` ON (`cash`.`toplayer` = `player`.`id`)
			LEFT JOIN `career` ON (`cash`.`tocareer` = `career`.`id`)
		WHERE `cash`.`player` = '$playerid' AND `cash`.`game` = '$current_game_id'
		ORDER BY `cash`.`datetime`");
	$settings = mysql_fetch_assoc(mysql_query("SELECT * FROM `settings` LIMIT 1"));
	// Initial cash
	$running_total = $settings['initialcash'];
	echo "
		<tr>
			<td>" . format_date_time($current_game['started']) . "</td>
			<td>" . format_money($running_total) . "</td>
			<td>Initial cash</td>
			<td>-</td>
			<td>-</td>
			<td>" . format_money($running_total) . "</td>
		</tr>";
	// Transactions
	while ($transaction = mysql_fetch_assoc($transactions_query)) {
		$running_total += $transaction['amount'];
		echo "
			<tr>
				<td>" . format_date_time($transaction['datetime']) . "</td>
				<td>" . format_money($transaction['amount']) . "</td>
				<td>" . $transaction['event'] . "</td>
				<td>" . ($transaction['tocareername']=="" ? "-" : $transaction['tocareername']) . "</td>
				<td>" . ($transaction['toplayername']=="" ? "-" : $transaction['toplayername']) . "</td>
				<td>" . format_money($running_total) . "</td>
			</tr>";
	}
?>
</table>

<?php include_once "footer.php"; ?>
